import { _decorator, Component, Node, SystemEvent, systemEvent, Touch, EventTouch, Enum } from 'cc';
import { Constant, EnemyType } from '../framework/Constant';
const { ccclass, property, executeInEditMode } = _decorator;

/**
 * Predefined variables
 * Name = PlaneEnemy
 * DateTime = Sat Jan 08 2022 12:16:47 GMT+0800 (中国标准时间)
 * Author = Kesa
 * FileBasename = planeEnemy.ts
 * FileBasenameNoExtension = planeEnemy
 * URL = db://assets/Scripts/plane/planeEnemy.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

// NOTE: 注册 枚举 EnemyType
Enum(EnemyType);

@ccclass('PlaneEnemy')
// 在 编辑器里运行脚本
// @executeInEditMode(true)
export class PlaneEnemy extends Component {
    // 敌机飞行速度
    @property
    public speed = 10;

    // 实例销毁边界
    @property
    public OUTOFBOUND = 100;

    // 当前飞行 z 坐标
    // @property
    public _currentZ = 0;

    // 敌机类型
    // NOTE: 使用枚举 EnemyType
    @property({ type: EnemyType })
    enemy_type: EnemyType = EnemyType.TYPE_A;

    private pos;
    private pos_z_init;

    start() {
        this.pos = this.node.position;
        this.pos_z_init = this.node.position.z;
    }

    update(deltaTime: number) {
        // 向 z轴正方向飞行
        this._currentZ = this.pos.z + this.speed * deltaTime;
        this.node.setPosition(this.pos.x, this.pos.y, this._currentZ);
        // console.log(this._currentZ);

        // 超过 实例销毁边界即 销毁本实例
        if (this._currentZ - this.pos_z_init > this.OUTOFBOUND) {
            {
                this.node.destroy();
            }
        }
    }

    show(speed: number) {
        this.speed = speed;
    }

    onDestroy() {
        // console.log('敌机销毁 Z 位置:', this._currentZ);
        // console.log('enemy instance destroyed...');
    }

    // -----------------------------------
}
